<?php

namespace app\model;

use think\facade\Cache;
use think\Model;

class BocNews extends Model
{
    protected $table = 'boc_news';

    /**
     * 上一篇 下一篇.
     */
    public function Page_news($id, $ctype = '')
    {
        $where[] = ['status', '=', 1];
        $where[] = ['is_deleted', '=', 0];
        $where[] = ['cate_id', '=', $ctype];
        $where[] = ['create_at', '<', date('Y-m-d H:i:s')];
        $list = self::field('id,title,cate_id,create_at,click_num')->where($where)->order('sort desc,create_at desc')->select()->toarray();

        $kkid = '';
        foreach ($list as $k => $v) {
            if ($v['id'] == $id) {
                $kkid = $k;
                break;
            }
        }

        $data['pre'] = '';
        $data['next'] = '';
        if (!empty($list[$kkid - 1])) {
            $data['pre'] = $list[$kkid - 1];
        }
        if (!empty($list[$kkid + 1])) {
            $data['next'] = $list[$kkid + 1];
        }

        return $data;
    }

    /*
     * 获取列表数据
     */
    public function get_list()
    {
        $cache_key = 'news-list';
        $list = Cache::get($cache_key);
        if (empty($list)) {
            $list = $this->field('id,title')
                ->where(['status' => 1, 'audit' => 0, 'is_deleted' => 0])
                ->order('sort desc,create_at desc')
                ->select()->toArray();
            Cache::tag('news')->set($cache_key, $list);
        }

        return $list;
    }

    public function get_new_list($limit = 9)
    {
        $cache_key = 'get_news_list';
        $list = Cache::get($cache_key);
        if (empty($list)) {
            $list = $this->field('id,title,cover')
                ->where(['status' => 1, 'audit' => 0, 'is_deleted' => 0])
                ->order('sort desc,create_at desc')
                ->limit($limit)
                ->select()->toArray();
            Cache::tag('news')->set($cache_key, $list);
        }

        return $list;
    }

    /**
     * 置顶新闻.
     */
    public function get_audit($cate_id, $limit = 4, $field = 'id,title,intro,photo,file,left(create_at,10) as create_at', $audit = 1)
    {
        $key = 'news_audit_'.$cate_id;
        $data = Cache::get($key);
        if (empty($data)) {
            $list = $this->field($field)->where(['audit' => $audit, 'is_deleted' => 0, 'status' => 1, 'ctype' => $cate_id])->limit($limit)->order('sort desc,create_at desc')->select();

            $data = $list ? $list->toarray() : [];
            Cache::tag('news')->set($key, $data);
        }

        return $data;
    }

    /**
     * 首页置顶新闻.
     */
    public function index_audit($field = 'id,title,cover,left(create_at,10) as create_at,substring(create_at,6,6) as d', $limit = 10, $audit = 1)
    {
        $key = 'news_index_audit';
        $data = Cache::get($key);
        $where[] = ['status', '=', 1];
        $where[] = ['is_deleted', '=', 0];
        $where[] = ['index_audit', '=', $audit];
        $where[] = ['create_at', '<', date('Y-m-d H:i:s')];
        if (empty($data)) {
            $list = $this->field($field)->where($where)->limit($limit)->order('sort desc,create_at desc')->select();
            $data = $list ? $list->toarray() : [];
            Cache::tag('news')->set($key, $data);
        }

        return $data;
    }

    public function type_list($cate_id, $field = 'id,title,cover,left(create_at,10) as create_at,substring(create_at,6,6) as d', $limit = 3, $audit = 1)
    {
        $key = 'news_type_list';
        $data = Cache::get($key);
        if (empty($data)) {
            $list = $this->field($field)->where(['cate_id' => $cate_id, 'is_deleted' => 0, 'status' => 1])->limit($limit)->order('sort desc,create_at desc')->select();
            $data = $list ? $list->toarray() : [];
            Cache::tag('news')->set($key, $data);
        }

        return $data;
    }

    /**
     * 首页置顶轮播新闻.
     */
    public function loop_audit($field = 'id,title,intro,thumb,left(create_at,10) as create_at,content,cate_id', $audit = 1)
    {
        $list = $this->field($field)->where(['loop_audit' => $audit, 'is_deleted' => 0, 'status' => 1])->order('sort desc,create_at desc')->select();

        return $list ? $list->toarray() : [];
    }

    public function get_one($id)
    {
        $key = 'new_detail_'.$id;
        $list = Cache::get($key);
        if (empty($list)) {
            $list = $this->field('id,cate_id,title_seo,description,keywords,title,content,left(create_at,10) as create_at,click_num')
                ->where(['is_deleted' => 0, 'status' => 1, 'id' => $id])->findOrEmpty()->toArray();
            Cache::tag('news')->set($key, $list);
        }

        return $list;
    }

    public function inc_click($id)
    {
        $key = 'new_detail_'.$id;
        $list = Cache::get($key);
        if (!empty($list)) {
            $list['click_num'] = $list['click_num'] + 1;
            Cache::tag('news')->set($key, $list);
        }

        return $this->where(['id' => $id])->inc('click_num')->update();
    }

    /**
     * 获取详情页随机数据.
     */
    public function Rand_list($id, $count = 3)
    {
        $where = [
            ['id', '<>', $id],
//            ['cate_id','=',$type],
            ['status', '=', '1'],
            ['is_deleted', '=', 0],
            ['create_at', '<', date('Y-m-d H:i:s')],
        ];

        return $this->field('id,cate_id,title,cover,left(create_at,10) as create_at')->where($where)->limit($count)->orderRaw('RAND()')->select()->toArray();
    }

    public function page_list($where = ['is_deleted' => 0, 'status' => 1], $field = 'id,title,cover,intro,left(create_at,10) as create_at', $list_rows = 6)
    {
        $list = $this->field($field)->where($where)->order('sort desc,create_at desc')->paginate(['list_rows' => $list_rows, 'query' => request()->param()]);

        return $list;
    }

    /**
     * 搜索.
     */
    public function get_search_list($where = ['status' => 1, 'audit' => 0, 'is_deleted' => 0])
    {
        $list = $this->field('id,title,intro,left(create_at,10) as create_at')
                ->where($where)
                ->order('sort desc,create_at desc')
                ->select()->toArray();

        return $list;
    }
}
